//好友boss系统
"use strict"
const mvc = require("cosjs.mvc")
const dbase = require("../dbase");
const promise = mvc.library.require("promise");

class fWboss extends dbase.gzone.mongodb {
    constructor(sid) {
        super(sid, 'fWboss');
    }
    //获取列表
    mget(uid, keys) {
        let query = { "shareList": { "$in": [uid] } };
        let option = { "multi": true, "dataType": "array", fields: this.fields(keys), "projection": { "shareList": 0 } };
        return this.find(query, option);
    }
    //获取boos数据
    get(bossID, keys) {
        let query = { "_id": bossID };
        // let option = { "multi": true, "projection": {} };
        // //拆分keys，如果长度大于1，那么就是多个key，塞入projection
        // if (keys) {
        //     let arr = keys.split(',');
        //     if (arr.length > 1) {
        //         for (let i in arr) {
        //             option['projection'][arr[i]] = 0;
        //         }
        //     }
        // }
        return this.find(query);
    }
    getData(uid, keys) {
        let key = "battleList." + uid + ".flag"
        let query = { "shareList": { "$in": [uid] } };
        query[key] = 0;
        // query.battleList[uid] = {};
        // query.battleList[uid].flag = 0;
        let option = { "multi": true, "dataType": "array", fields: this.fields(keys), "projection": { "shareList": 0 } };
        return this.find(query, option);
        // let option = { "multi": true, "projection": {} };
        // //拆分keys，如果长度大于1，那么就是多个key，塞入projection
        // if (keys) {
        //     let arr = keys.split(',');
        //     if (arr.length > 1) {
        //         for (let i in arr) {
        //             option['projection'][arr[i]] = 0;
        //         }
        //     }
        // }
        return this.find(query);
    }
    getMyboss(uid) {
        let query = { "uid": uid };
        // let option = { "multi": true, "projection": {} };
        // //拆分keys，如果长度大于1，那么就是多个key，塞入projection
        // if (keys) {
        //     let arr = keys.split(',');
        //     if (arr.length > 1) {
        //         for (let i in arr) {
        //             option['projection'][arr[i]] = 0;
        //         }
        //     }
        // }
        return this.find(query, option);
    }
    //添加邮件
    add(uid, data) {
        let info = boss_format.call(this, uid, data);
        return this.insert(info);
    }
    //清理数据
    clear() {
        let nowTime = Date.now();
        let expireTime = nowTime - 30 * 86400 * 1000;
        let query = { "findTime": { "$lte": expireTime } };
        let option = { "multi": true };
        return this.remove(query, option);
    }

    // //系统邮件,id,data......
    // sys(uid, id, data, ...arr) {
    //     let sysMail = mvc.config('mail', id);
    //     if (!sysMail) {
    //         return promise.callback('config_not_exist', ['mail', id]);
    //     }
    //     data['type'] = 1;
    //     arr.unshift(id);
    //     for (let i in arr) {
    //         arr[i] = encodeURIComponent(arr[i]);
    //     }
    //     data['title'] = arr.join(',');
    //     data['content'] = '';
    //     return this.add(uid, data);
    // }

}


function boss_format(uid) {
    let data = Object.assign({}, arguments[1]);
    if (!data['uid']) {
        data['uid'] = uid;
    }
    if (!data['findTime']) {
        data['findTime'] = Date.now();
    }
    if (!data['startTime']) {
        data['startTime'] = Date.now();
    }
    mvc.format('fWboss', data, true);
    if (!data['_id']) {
        data['_id'] = uid + data['findTime'].toString(32);
    }
    return data;
}


module.exports = function (sid) {
    return new fWboss(sid);
};


//索引
module.exports.indexes = [
    [{ "uid": 1 }, {}],
    [{ "bossID": 1 }, {}]
]